package com.huqing.icu.rentgateway.aop;

import com.huqing.icu.base.redis.Redis;
import com.huqing.icu.client.UserFeignClient;
import com.huqing.icu.enums.user.UserNumPanelTypeEnum;
import com.huqing.icu.req.UserNumIncrReq;
import com.huqing.icu.utils.DateUtils;
import com.huqing.icu.utils.IpUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.sql.Timestamp;
import java.util.Arrays;

/**
 * @Description 用户在线AOP，获取用户在线人数
 * @Author huqing
 * @Date 2025/9/3 22:33
 **/
@Component
@Aspect
public class UserOnlineAop {

    @Autowired
    private Redis redis;

    @Resource
    private UserFeignClient userFeignClient;

    @Pointcut("@within(org.springframework.web.bind.annotation.RestController) || @annotation(org.springframework.web.bind.annotation.RestController)")
    public void requestServer() {
    }


    @Around("requestServer()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        Object result = proceedingJoinPoint.proceed();
        String ip = IpUtil.getIpAddress(request);

        //获取用户ID
        Long userId = (Long) request.getAttribute("userId");


        redis.hset("user_online", userId.toString(), 1);


        String today = DateUtils.timestampToStr(new Timestamp(System.currentTimeMillis()), DateUtils.yyyyMMDDWithBar);

        // 定义存储日活用户的Bitmap键名，格式可以是 "daily_active_users:日期"，这里假设日期为2025-09-04
        String dailyActiveUsers = "daily_active_users:" + today;

        // 将每个用户ID对应的位设置为1，表示该用户当天活跃，使用SETBIT命令，将用户ID对应的位设置为1
        redis.setbit(dailyActiveUsers, userId, true);

        //月度活跃用户，
        String month = DateUtils.timestampToStr(new Timestamp(System.currentTimeMillis()), DateUtils.yyyyMMWithBar);
        String monthActiveUsers = "month_active_users:" + month;
        redis.setbit(monthActiveUsers, userId, true);

        //年度活跃用户，
        String year = DateUtils.timestampToStr(new Timestamp(System.currentTimeMillis()), DateUtils.yyyyWithBar);
        String yearActiveUsers = "year_active_users:" + year;
        redis.setbit(yearActiveUsers, userId, true);

        // 统计日活、月活、年活用户数量，使用BITCOUNT命令
        Long dailyActiveUsersCount = redis.bitcount(dailyActiveUsers);
        Long monthActiveUsersCount = redis.bitcount(monthActiveUsers);
        Long yearActiveUsersCount = redis.bitcount(yearActiveUsers);

        //每日、每月、每年新增人数
        UserNumIncrReq incrReq = new UserNumIncrReq();
        incrReq.setDateList(Arrays.asList(today, month, year));
        incrReq.setTypeList(UserNumPanelTypeEnum.getAllCode());
        userFeignClient.incrUserNum(incrReq);

        return result;
    }

}
